
import { ref } from 'vue'
import type { Ref } from 'vue'
export function useDebounce<T>(value: T, delay = 500) {
  const debouncedValue = ref(value) as Ref<T>
  let timer: ReturnType<typeof setTimeout> | null = null

  const debounce = (newValue: T) => {
    if (timer) clearTimeout(timer)
    timer = setTimeout(() => {
      debouncedValue.value = newValue
    }, delay)
  }

  return {
    debouncedValue,
    debounce
  }
}

